//Lou Decroo
//updated 12-04-09

import java.awt.*;
import java.awt.event.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

import javax.swing.*;
import javax.swing.event.*;

public class ViewResultsGUI extends JPanel implements ActionListener{
	
	private JPanel resultsGrid, openFilePanel, dlResults;
	private ResultPanel[] rPanels;
	private JButton downloadResultsBtn;
	private JFileChooser fileChooser;
	private Result[] results;

	// constructor
	public ViewResultsGUI(){
		dlResults = new JPanel(new FlowLayout());
		fileChooser = new JFileChooser();
		openFilePanel = new JPanel();
		this.setLayout(new BorderLayout());

		// set some results
		/*String[] pos1 = {"Aight","Okay","So-so","Not so good","Wish I never had to program again"};
		double[] d1 = new double[5];
		d1[0] = 10;
		d1[1] = 0;
		d1[2] = 10;
		d1[3] = 20;
		d1[4] = 60;
		Result res1 = new Result("How are you?", 2, pos1, d1);
		String[] rasdf2 = {"Remove","It","From","The","Required","Curriculum"};
		Result res2 = new Result("How can this course be improved?", 3, rasdf2);
		Result[] r1 = {res1, res2};
		setResults(r1);
		*/

	}
	
	/**
	 * method to set results 
	 * @param res - an array of results objects
	 */
	public void setResults(Result[] res){
		// need to set the array of results here
		results = res;
		
		downloadResultsBtn = new JButton("Download Results");
		downloadResultsBtn.addActionListener(this);
		resultsGrid = new JPanel(new GridLayout(results.length+1, 1));
		
		rPanels = new ResultPanel[100]; // initialize size based on questions array from db
		

			System.out.println(results.length);
			for(int i = 0; i<results.length; i++){
				rPanels[i] = new ResultPanel(results[i], i+1);
				resultsGrid.add(rPanels[i]);
			}
			
		dlResults.add(downloadResultsBtn);
		this.add(resultsGrid, BorderLayout.CENTER);
		this.add(dlResults, BorderLayout.SOUTH);
		this.setSize(500, 500);
		this.setVisible(true);
	}

	/**
	 * Method required for action listeners
	 */
	public void actionPerformed(ActionEvent e){
		if(e.getSource() == downloadResultsBtn){
			int returnVal = fileChooser.showSaveDialog(openFilePanel);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fileChooser.getSelectedFile();
				//This is where a real application would save the file.
				JOptionPane.showMessageDialog(null, "Saving: " + file.getName() + "." + "\n");
				writeFile(file);
				
			} 
		}
	}
	
	/**
	 * method to create and write to a file
	 * @param file - file to write to
	 */
	public void writeFile(File file){
	    FileOutputStream fos; 
	    DataOutputStream dos;
	    String str = null;

	    try {
	      fos = new FileOutputStream(file);
	      dos=new DataOutputStream(fos);
	      for(int i =0; i<results.length; i++)
	    	  dos.writeChars(i+1 + ". " + results[i].toString());

	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	}
	
	public static void main(String[] args){
		JFrame f = new JFrame();
		JScrollPane scroll = new JScrollPane();
		
		scroll = new JScrollPane(new ViewResultsGUI());
		scroll.getVerticalScrollBar();
		scroll.getHorizontalScrollBar();
		f.add(scroll);
		f.setSize(1000, 1000);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

}
